package cn.edu.sjtu.software.sjtucontacts.util;

import cn.edu.sjtu.software.sjtucontacts.R;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ContactNumberItem extends RelativeLayout implements OnClickListener{

	private BasicContactItem basicInfo;
	private ImageButton callButton,smsButton;
	
	public ContactNumberItem(Context context) {
		super(context);
		basicInfo = new BasicContactItem(context);
		this.addView(basicInfo);
		RelativeLayout.LayoutParams params = 
			(RelativeLayout.LayoutParams)basicInfo.getLayoutParams();
		params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		basicInfo.setLayoutParams(params);
		
		LinearLayout actionsField = new LinearLayout(context);
		actionsField.setGravity(Gravity.CENTER_VERTICAL);
		
		callButton = new ImageButton(context);
		callButton.setImageResource(R.drawable.badge_action_call);
		callButton.setBackgroundColor(0x00000000);
		smsButton = new ImageButton(context);
		smsButton.setImageResource(R.drawable.badge_action_sms);
		smsButton.setBackgroundColor(0x00ffffff);
		LinearLayout divider = new LinearLayout(context);
		divider.setBackgroundColor(0xffffffff);
		TextView tmp = new TextView(context);
		//tmp.setText("s");
		divider.addView(tmp);
		
		actionsField.addView(callButton);
		callButton.setOnClickListener(this);
		actionsField.addView(divider);
		LinearLayout.LayoutParams dividerParams = 
			(LinearLayout.LayoutParams)divider.getLayoutParams();
		dividerParams.width=1;
		dividerParams.height=30;
		dividerParams.gravity = Gravity.CENTER_VERTICAL;
		//dividerParams.topMargin=3;
		//dividerParams.bottomMargin=3;
		divider.setLayoutParams(dividerParams);
		actionsField.addView(smsButton);
		smsButton.setOnClickListener(this);
		
		this.addView(actionsField);
		params = 
			(RelativeLayout.LayoutParams)actionsField.getLayoutParams();
		params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		actionsField.setLayoutParams(params);
	}
	
	public void setNumberType(String type){
		basicInfo.setType(type);
	}
	
	public void setNumber(String number){
		basicInfo.setValue(number);
	}

	/**
     * Kick off an intent to initiate a call.
     *
     * @param phoneNumber must not be null.
     * @throws NullPointerException when the given argument is null.
     */
    public static void initiateCall(Context context, CharSequence phoneNumber) {
        Intent intent = new Intent(Intent.ACTION_CALL,
                Uri.fromParts("tel", phoneNumber.toString(), null));
        context.startActivity(intent);
    }
    
    /**
     * Kick off an intent to initiate an Sms/Mms message.
     *
     * @param phoneNumber must not be null.
     * @throws NullPointerException when the given argument is null.
     */
    public static void initiateSms(Context context, CharSequence phoneNumber) {
        Intent intent = new Intent(Intent.ACTION_SENDTO,
                Uri.fromParts("sms", phoneNumber.toString(), null));
        context.startActivity(intent);
    }
	
	@Override
	public void onClick(View v) {
		if(v == callButton){
			initiateCall(this.getContext(),basicInfo.getValue());
			System.out.println("make a call now");
		}else if(v == smsButton){
			initiateSms(this.getContext(),basicInfo.getValue());
		}
	}

}
